package com.wars2donline.client.actor;

import java.util.ArrayList;
import java.util.List;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.wars2donline.client.data.HeroData;
import com.wars2donline.client.util.Global;

public class HeroActor extends Actor {
	
	enum HeroState {
		IDLE, WALK, JUMP, ATTACK
	}
	
	HeroState mCurrentState = HeroState.IDLE;
	
	HeroData mData;
	
	Animation mIdleAnimation;
	Animation mWalkAnimation;
	
	final float mAnimFrameDuration = 1.0f / 3.0f;  
	
	TextureAtlas mAnimAtlas = null;
	
	float stateTime = .0f;

	private float mSpeed = 100.0f;
	
	boolean flipped = false; 
	
	public HeroActor(HeroData pData) {
		mData = pData;
		
		TextureAtlas atlas = new TextureAtlas(Gdx.files.internal(Global.HERO_DIR + pData.id + "/anim/idle/pack"));
		
		List<TextureRegion> idleAnimRegions = new ArrayList<TextureRegion>();
		List<TextureRegion> walkAnimRegions = new ArrayList<TextureRegion>();
		
		for (int i : pData.idleAnimFrames) {
			idleAnimRegions.add(atlas.findRegion(String.valueOf(i)));
		}
		
		atlas = new TextureAtlas(Gdx.files.internal(Global.HERO_DIR + pData.id + "/anim/walk/pack"));
		
		for (int i : pData.walkAnimFrames) {
			walkAnimRegions.add(atlas.findRegion(String.valueOf(i)));
		}
		
		mIdleAnimation = new Animation(mAnimFrameDuration, idleAnimRegions);
		mWalkAnimation = new Animation(mAnimFrameDuration, walkAnimRegions);
	}
	
	@Override
	public void draw(SpriteBatch arg0, float arg1) {
		
		TextureRegion drawingRegion = null;
		
		switch (mCurrentState) {
		case IDLE:
			drawingRegion = mIdleAnimation.getKeyFrame(stateTime, true);
			break;
			
		case WALK:
			drawingRegion = mWalkAnimation.getKeyFrame(stateTime, true); 
			break;
		}
		
		if (flipped) {
			if (!drawingRegion.isXFlipped) {
				drawingRegion.flip(true, false);
			}
		} else {
			if (drawingRegion.isXFlipped) {
				drawingRegion.flip(true, false);
			}
		}
		
		arg0.draw(drawingRegion, x, y);
	}

	@Override
	public Actor hit(float arg0, float arg1) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void act(float delta) {
		super.act(delta);
		
		stateTime += delta;
	}

	public void change() {
		if (mCurrentState == HeroState.IDLE) {
			mCurrentState = HeroState.WALK;
		} else {
			mCurrentState = HeroState.IDLE;
		}
	}

	public void walkRight(float deltaTime) {
		flipped = false;
		x += mSpeed  * deltaTime;
		mCurrentState = HeroState.WALK;
	}
	
	public void walkLeft(float deltaTime) {
		turnLeft();
		x -= mSpeed * deltaTime;
		mCurrentState = HeroState.WALK;
	}
	
	private void turnLeft() {
		flipped = true;
	}

	public void setIdleState() {
		mCurrentState = HeroState.IDLE;
	}

}
